# -*- coding:utf-8 -*-

from flask import g
from flask_wtf import Form
from models import User,Deportes
from wtforms.validators import DataRequired, Required
from wtforms import Form, BooleanField, TextField, PasswordField, DateField, DecimalField, validators
from wtforms.ext.sqlalchemy.fields import QuerySelectField,QuerySelectMultipleField
from werkzeug import secure_filename,FileStorage
from flask_wtf.file import FileField, FileAllowed, FileRequired

def enabled_sports_for_user():
    return Deportes.query.filter(Deportes.user.any(id=g.user.get_id())).all()

def enabled_sports():
    return Deportes.query.all()

class LoginForm(Form):
    openidTxt = TextField('openid', validators = [DataRequired()])
    remember_me = BooleanField('remember_me', default = False)
    
class DietaFilter(Form):
    ingredientes = TextField('Ingredientes', [validators.Length(min=5, max=64)])
        
class UserProfile(Form):
    apodo = TextField('Apodo', [validators.Length(min=5, max=64)])
    nombre = TextField('Nombre', [validators.Length(min=1, max=100)])
    apellido = TextField('Apellido', [validators.Length(min=5, max=100)])
    fechaNacimiento = DateField('Fecha Nacimiento', validators=[DataRequired()],format='%d/%m/%Y')
    email = TextField('Email', [DataRequired(),validators.Email('Email invalido.')])
    direccion = TextField('Direccion')
    photo = FileField('Imagen de perfil',validators=[
        FileRequired()#,
        #FileAllowed(photos, 'Solo imagenes')
    ])
    

class AddActivityForm(Form):
    deportes = QuerySelectField('Deporte',query_factory=enabled_sports_for_user, allow_blank=True, blank_text=('-- Select --'), get_label='nombre')
    distancia =  TextField('Distancia',[validators.Regexp('[\d\,\.]+',flags=0,message="Ingrese nuevamente la distancia en km") ])
    duracion = TextField('Duracion',[validators.Regexp('[\d\,\.]+',flags=0,message="Ingrese nuevamente la duracion en min") ])
    fecha = DateField('Fecha',[validators.required()])

class AddCompetitionForm(Form):
    nombre = TextField('Titulo',[validators.Length(min=1, max=100)])
    introduccion = TextField('Introduccion',[validators.Length(min=1, max=500)])
    fecha = DateField('Fecha',validators=[DataRequired()], format='%d/%m/%Y')
    deportes = QuerySelectField('Deporte',query_factory=enabled_sports_for_user, allow_blank=True, blank_text=('-- Select --'), get_label='nombre')

